import React, { useState } from 'react'

// 把 timer 作为全局变量放到外面，肯定能保证是同一个 timer
let timer = null
export default function Test() {
  const [count, setCount] = useState(10)
  const handleClick = () => {
    console.log(timer, 333)
    clearInterval(timer)
    timer = setInterval(() => {
      setCount((count) => count - 1)
    }, 1000)
  }
  return (
    <div>
      <h3>{count}</h3>
      <button onClick={handleClick}>倒计时</button>
    </div>
  )
}
